import pandas as pd
import wordcloud
import matplotlib.pyplot as plt #绘制图片
import jieba
import jieba.analyse as ana
import imageio
from nltk import FreqDist

#使用自定义词典
def dict_words(raw):
    dict = 'G:/python/lvyou/zhang/little/user_dict.txt'
    jieba.load_userdict(dict)  #增加词典
    for i in range(len(raw)):
        # print('//'.join(jieba.cut(raw.txt[i])))
        return raw

#停用词
def dele_words(raw):
    ana.set_stop_words('G:/python/lvyou/zhang/little/stoplist.txt')
    words_list = []
    for i in range(len(raw)):
        #print(ana.extract_tags(raw.txt[i]))
        lists = ana.extract_tags(raw.txt[i])
        #print(lists)
        words_list.extend(lists)
    return words_list

#词频统计
def cal_words(words_list):
    df = pd.DataFrame(words_list, columns=['word'])
    result =df.groupby(['word']).size()
    #print(type(result))
    freqlist = result.sort_values(ascending=False) #降序
    print(freqlist[:20])

#绘制词云图
def pic_words(words_list):
    fdist = FreqDist(words_list)
    #pic_address = path.abspath('timg.jpg'),
    img_array = imageio.imread('G:/python/lvyou/zhang/templates/img/222.jpg') # 读取图片
    pic_color = wordcloud.ImageColorGenerator(img_array) # ImageColorGenerator函数将词云的颜色参数设置为根据图片颜色确定
    cloudobj = wordcloud.WordCloud(font_path = "simkai.ttf",
                                   height = 500, width = 500,
                                   mode = "RGBA",
                                   background_color = 'black',
                                   mask = img_array,
                                   color_func = pic_color, #图片主色调为蓝灰色
                                  ).fit_words(fdist)

    plt.imshow(cloudobj)
    plt.axis("off")
    plt.show()
    #cloudobj.to_file("D:/pyhomework/0516/dig1/pic1.png") 保存的图片为1：20行
    cloudobj.to_file("G:/python/lvyou/zhang/static/huangguoshu.png")

if __name__ == '__main__':
    raw = pd.read_csv("G:/python/lvyou/zhang/templates/txt/huangguoshu.txt", names=['txt'], sep='aaa', encoding="utf-8")
    print(len(raw))
    dict_words(raw)
    words_list = dele_words(raw)
    cal_words(words_list)
    pic_words(words_list)
